﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manzana;
using IVR3Downloader.Class;
using System.IO;

namespace IVR3Downloader.Forms
{
    public partial class FormIVR3 : Form
    {
        

        public FormIVR3()
        {
            InitializeComponent();

            IVR3.Instance.BackgroundWorker = this.backgroundWorkerIVR3;
            IVR3.OnConnect += new IVR3.Connect(Ivr3iPhone_OnConnect);
            IVR3.OnDisconnect += new IVR3.Disconnect(IVR3_OnDisconnect);
        }

        private void IVR3_OnDisconnect()
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.IVR3_OnDisconnect();
                });
            }

            this.backgroundWorkerIVR3.CancelAsync();

            labelIphoneStatus.Text = "iPhone status: Disconnected";
            labelIphoneStatus.ForeColor = Color.Green;
            movieBindingSource.Clear();

        }

        private void Ivr3iPhone_OnConnect()
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                   this.Ivr3iPhone_OnConnect();
                });
                return;
            }

            labelIphoneStatus.Text = "iPhone status: Connected";
            labelIphoneStatus.ForeColor = Color.Green;
            this.UpdateVideoList();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            backgroundWorkerIVR3.RunWorkerAsync();


        }

        private void backgroundWorkerIVR3_DoWork(object sender, DoWorkEventArgs e)
        {
            Movie[] moviesToCopy = this.movieBindingSource.List.Cast<Movie>().Where(m => m.IsChecked).ToArray();

            try
            {
                IVR3.Instance.Copy(moviesToCopy, e.Argument.ToString());
            }
            catch (Exception ex)
            {
               
                throw ex;
            }
        }

        private void backgroundWorkerIVR3_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            object objProg = e.UserState;

            if (objProg.GetType() == typeof(ProgressionFile))
            {
                ProgressionFile p = (ProgressionFile)e.UserState;

                if (p.FileTotal == 0)
                {
                    labelFileProg.Text = "File: N/A";
                    progressBarFile.Value = 0;
                }
                else
                {
                    labelFileProg.Text = String.Format("File: {0} on {1} file(s) downloaded. | {2:0.00} %", p.FileDone, p.FileTotal, p.FileNumberPercentage);
                    progressBarFile.Value = (int)p.FileNumberPercentage;
                }
            }

            if (objProg.GetType() == typeof(ProgressionDownload))
            {
                ProgressionDownload p = (ProgressionDownload)e.UserState;

                if (p.DownloadError != null)
                {
                    MessageBox.Show(this, p.DownloadError, "Download failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (p.FileSize == 0)
                {
                    labelDownProg.Text = "Download: N/A";
                    progressBarDownload.Value = 0;
                }
                else
                {
                    labelDownProg.Text = String.Format("Download: {0} on {1} downloaded. | {2:0.00} %", p.UserFileSizeDownloaded, p.UserFileSize, p.FileSizePercentage);
                    progressBarDownload.Value = (int)p.FileSizePercentage;
                }
            }
            
        }

        private void backgroundWorkerIVR3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            buttonCopy.Text = "Copy";
            buttonCopy.Enabled = true;
            buttonRefresh.Enabled = true;
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            if (buttonCopy.Text.Equals("Copy"))
            {
                
                String destinationFolder = Path.Combine(Path.GetFullPath(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)),"iPhoneVideoRecorder");
                Directory.CreateDirectory(destinationFolder);

                this.backgroundWorkerIVR3.RunWorkerAsync(destinationFolder);
                buttonCopy.Text = "Cancel";
                buttonRefresh.Enabled = false;
            }
            else
            {

                this.backgroundWorkerIVR3.CancelAsync();
                buttonCopy.Enabled = false;
            }
        }

        private void FormIVR3_Load(object sender, EventArgs e)
        {
           
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            if (IVR3.Instance.IsConnected)
            {
                this.UpdateVideoList();
            }
            else
            {
                MessageBox.Show("No iPhone connected !");
            }
        }

        private void UpdateVideoList()
        {
            //List the video in IVR3 folder.
            Movie[] movies = IVR3.Instance.ListVideos();

            //Clear the binding source.
            movieBindingSource.Clear();

            //Add each movie found on the iPhone on the bidingsource.
            foreach (Movie m in movies)
            {
                movieBindingSource.Add(m);
            }            
        }
        


    }
}
